<?php
declare (strict_types=1);

namespace app\admin\service;

use app\admin\model\MarketSocialMediaAccount as MarketSocialMediaAccountModel;
use app\admin\model\SystemSmChannel as SystemSmChannelModel;
use app\admin\model\SystemLanguage as SystemLanguageModel;
use app\admin\model\RegionCountry as RegionCountryModel;
use app\admin\model\MarketSocialMediaAccountGroup as MarketSocialMediaAccountGroupModel;

class MarketSocialMediaAccount extends BaseService
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 获取列表数据
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 14:48:35
     * author: jiang
     */
    public function index($param, $page, $limit)
    {
        $arrData = ['code' => 0, 'message' => 'success'];
        $where = [['is_del', '=', 0]];
        if ($param['account']) {
            $where[] = ['account', 'like', '%' . $param['account'] . '%'];
        }
        if ($param['status'] != null && $param['status'] != '') {
            $where[] = ['status', '=', $param['status']];
        }

        if (!empty($param['channel_id'])) {
            $where[] = ['channel_id', 'in', $param['channel_id']];
        }

        if (!empty($param['language'])) {
            $where[] = ['language', 'in', $param['language']];
        }
        if (!empty($param['country'])) {
            $where[] = ['country', 'in', $param['country']];
        }

        if (!empty($param['account_group_id'])) {
            $where[] = ['account_group_id', 'in', $param['account_group_id']];
        }

        if ($param['start_time']) {
            $where[] = ['create_time', '>=', $param['start_time']];
        }
        if ($param['end_time']) {
            $where[] = ['create_time', '<', $param['end_time']];
        }

        $oMarketSocialMediaAccountModel = MarketSocialMediaAccountModel::with(['get_creator_name', 'get_update_name'])->where($where);
        $result = $oMarketSocialMediaAccountModel->page($page, $limit)->order('id', 'desc')->select();

        $total = MarketSocialMediaAccountModel::where($where)->count();
        foreach ($result as &$value) {
            $value->creator = $value->creator_value;
            unset($value->creator_value);
            $value->updater = $value->updater_value;
            unset($value->updater_value);
        }
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 获取详情数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 15:23:47
     * author: jiang
     */
    public function read($param)
    {
        $where = [['id', '=', $param['id']]];
        $oMarketSocialMediaAccountModel = MarketSocialMediaAccountModel::with(['get_creator_name', 'get_update_name'])->where($where)->find();
        if (!$oMarketSocialMediaAccountModel) {
            return ['code' => 1, 'message' => '获取信息失败！'];
        }
        $oMarketSocialMediaAccountModel->creator = $oMarketSocialMediaAccountModel->creator_value;
        unset($oMarketSocialMediaAccountModel->creator_value);
        $oMarketSocialMediaAccountModel->updater = $oMarketSocialMediaAccountModel->updater_value;
        unset($oMarketSocialMediaAccountModel->updater_value);
        return ['code' => 0, 'message' => 'success', 'data' => $oMarketSocialMediaAccountModel];
    }

    /**
     * 添加数据
     * @param $param
     * @return array
     * 2024-10-28 15:30:26
     * author: jiang
     */
    public function save($param)
    {
        // 判断账号是否重复
        $count = MarketSocialMediaAccountModel::where([['account', '=', $param['account']], ['is_del', '=', 0]])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '账号重复！'];
        }
        $oMarketSocialMediaAccountModel = new MarketSocialMediaAccountModel();
        $oMarketSocialMediaAccountModel->channel_id = $param['channel_id'];
        $oMarketSocialMediaAccountModel->language = $param['language'];
        $oMarketSocialMediaAccountModel->country = $param['country'];
        $oMarketSocialMediaAccountModel->account = $param['account'];
        $oMarketSocialMediaAccountModel->pass = $param['pass'];
        $oMarketSocialMediaAccountModel->appkey = $param['appkey'];
        $oMarketSocialMediaAccountModel->secret = $param['secret'];
        $oMarketSocialMediaAccountModel->prompt = $param['prompt'];
        $oMarketSocialMediaAccountModel->account_group_id = $param['account_group_id'];
        $oMarketSocialMediaAccountModel->windows_id = $param['windows_id'];

        $oMarketSocialMediaAccountModel->status = $param['status'];
        $oMarketSocialMediaAccountModel->is_del = 0;
        $oMarketSocialMediaAccountModel->creator = $param['operator_id'];
        $oMarketSocialMediaAccountModel->create_time = date('Y-m-d H:i:s');
        if (!$oMarketSocialMediaAccountModel->save()) {
            return ['code' => 1, 'message' => '添加失败！'];
        }
        $this->logger->logOperation($oMarketSocialMediaAccountModel->id,'MarketSocialMediaAccount','添加','添加社媒账号');
        return ['code' => 0, 'message' => '添加成功！'];
    }

    /**
     * 修改数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 15:50:28
     * author: jiang
     */
    public function update($param)
    {
        $oMarketSocialMediaAccountModel = MarketSocialMediaAccountModel::find($param['id']);
        if (!$oMarketSocialMediaAccountModel) {
            return ['code' => 1, 'message' => '数据不存在！'];
        }
        // 判断name是否重复
        $count = MarketSocialMediaAccountModel::where([['account', '=', $param['account']], ['is_del', '=', 0]])->where('id', '<>', $param['id'])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '账号重复！'];
        }

        $oSystemSmChannelModel = SystemSmChannelModel::find($param['channel_id']);
        if (!$oSystemSmChannelModel) {
            return ['code' => 1, 'message' => '渠道不存在！'];
        }

        $sign = $oSystemSmChannelModel->name;
        $encodestr = base64_encode($param['pass'] . $sign);

        $oMarketSocialMediaAccountModel->channel_id = $param['channel_id'];
        $oMarketSocialMediaAccountModel->language = $param['language'];
        $oMarketSocialMediaAccountModel->country = $param['country'];
        $oMarketSocialMediaAccountModel->account = $param['account'];
        $oMarketSocialMediaAccountModel->pass = $encodestr;

        $oMarketSocialMediaAccountModel->appkey = $param['appkey'];
        $oMarketSocialMediaAccountModel->secret = $param['secret'];
        $oMarketSocialMediaAccountModel->status = $param['status'];
        $oMarketSocialMediaAccountModel->prompt = $param['prompt'];
        $oMarketSocialMediaAccountModel->windows_id = $param['windows_id'];

        $oMarketSocialMediaAccountModel->updater = $param['operator_id'];
        $oMarketSocialMediaAccountModel->update_time = date('Y-m-d H:i:s');
        if (!$oMarketSocialMediaAccountModel->save()) {
            return ['code' => 1, 'message' => '修改失败！'];
        }
        $this->logger->logOperation($oMarketSocialMediaAccountModel->id,'MarketSocialMediaAccount','修改','修改社媒账号');
        return ['code' => 0, 'message' => '修改成功！'];
    }

    /**
     * 删除数据
     * @param $param
     * @return array
     * 2024-10-28 16:18:34
     * author: jiang
     */
    public function delete($param)
    {
        $result = MarketSocialMediaAccountModel::whereIn('id', $param['ids'])->update([
            'is_del' => time(),
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '删除失败！'];
        }
        $this->logger->logOperation($param['ids'],'MarketSocialMediaAccount','删除','删除社媒账号');
        return ['code' => 0, 'message' => '删除成功！'];
    }

    /**
     * 获取资源
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 16:45:27
     * author: jiang
     */
    public function resource($param)
    {
        $data = [];
        $channel = SystemSmChannelModel::where([['is_del', '=', 0], ['status', '=', 1]])->field(['name as label', 'id as value']);
        $language = SystemLanguageModel::where([['is_del', '=', 0], ['status', '=', 1]])->field(['english_name as label', 'id as value']);
        $country = RegionCountryModel::field(['name as label', 'id as value']);
        $account_group = MarketSocialMediaAccountGroupModel::where('is_del', 0)->field(['name as label', 'id as value']);

        switch ($param['field']) {
            case 'channel':
                $data['channel'] = $channel->select()->toArray();
                break;
            case 'language':
                $data['language'] = $language->select()->toArray();
                break;
            case 'country':
                $data['country'] = $country->select()->toArray();
                break;
            case 'account_group':
                $data['account_group'] = $account_group->select()->toArray();
                break;
            default:
                $data['channel'] = $channel->select()->toArray();
                $data['language'] = $language->select()->toArray();
                $data['country'] = $country->select()->toArray();
                $data['account_group'] = $account_group->select()->toArray();
        }

        return [
            'code' => 0,
            'data' => $data
        ];
    }

    public function status($param)
    {
        $result = MarketSocialMediaAccountModel::whereIn('id', $param['ids'])->update([
            'status' => $param['status'],
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '操作失败！'];
        }
        $this->logger->logOperation($param['ids'],'MarketSocialMediaAccount','修改','修改社媒账号状态');
        return ['code' => 0, 'message' => '操作成功！'];
    }
}
